#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 13-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
using Thn.Interface.Vcl;
#endregion

namespace Demo
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG
    public
#else
    public
#endif
 partial class LoginPage : Page
    {
        #region Fields
        #endregion

        #region Properties
        #endregion

        #region Login
        void Login()
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            if (SecurityModel.Login(username, password))
            {
                #region Logged in successfully

                lblError.Visible = false;
                UpdateUI();

                #endregion
            }
            else
            {
                #region Logging failed

                //show error to user
                lblError.Visible = true;
                blinker.Blink();
                #endregion
            }
        }
        #endregion

        #region Update UI
        void UpdateUI()
        {
            if (SecurityModel.IsAuthenticated)
            {
                #region Already logged in
                lblRole.Text = string.Format("You are logged in as {0}", SecurityModel.Role);
                pnlLogIn.Visible = false;
                pnlLogOff.Visible = true;
                pnlLogOff.Location = new Point(35, 95);
                #endregion
            }
            else
            {
                #region Not logged in yet
                pnlLogIn.Visible = true;
                pnlLogOff.Visible = false;
                pnlLogIn.Location = new Point(35, 95);
                #endregion
            }
        }
        #endregion

        #region UI Events processing

        #region Login
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }
        #endregion

        #region Logoff
        private void btnLogoff_Click(object sender, EventArgs e)
        {
            SecurityModel.LogOff();
            UpdateUI();
        }
        #endregion

        #region Enter Key
        private void EnterKey(object sender, EventArgs e)
        {
            Log.Debug("EnterKey - {0}", sender);
            if (!SecurityModel.IsAuthenticated)
            {
                Login();
            }
            #region Defensive tracing
            else
            {
                //no need to do mimic Login click because user is already logged in
            }
            #endregion
        }
        #endregion

        #endregion

        #region On Load
        /// <summary>
        /// Initialize this form when form is loaded.
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            if (!DesignMode)
            {
                //pnlLogOff.Location = pnlLogIn.Location;
                UpdateUI();
                lblError.Visible = false;
            }
            #region Defensive tracing
            else
            {
                //don't do anything at design-time
            }
            #endregion

            //let my ancestor process its own logic
            base.OnLoad(e);
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public LoginPage()
        {
            InitializeComponent();
        }
        #endregion
    }
}
